package com.castle.fortress.admin.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.goods.entity.DeliveryMethodEntity;
import com.castle.fortress.admin.goods.dto.DeliveryMethodDto;
import com.castle.fortress.admin.goods.mapper.DeliveryMethodMapper;
import com.castle.fortress.admin.goods.service.DeliveryMethodService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 配送方式 服务实现类
 *
 * @author whc
 * @since 2022-12-09
 */
@Service
public class DeliveryMethodServiceImpl extends ServiceImpl<DeliveryMethodMapper, DeliveryMethodEntity> implements DeliveryMethodService {
    /**
    * 获取查询条件
    * @param deliveryMethodDto
    * @return
    */
    public QueryWrapper<DeliveryMethodEntity> getWrapper(DeliveryMethodDto deliveryMethodDto){
        QueryWrapper<DeliveryMethodEntity> wrapper= new QueryWrapper();
        if(deliveryMethodDto != null){
            DeliveryMethodEntity deliveryMethodEntity = ConvertUtil.transformObj(deliveryMethodDto,DeliveryMethodEntity.class);
            wrapper.like(StrUtil.isNotEmpty(deliveryMethodEntity.getName()),"name",deliveryMethodEntity.getName());
            wrapper.eq(deliveryMethodEntity.getBillingMethod() != null,"billing_method",deliveryMethodEntity.getBillingMethod());
            wrapper.eq(deliveryMethodEntity.getIsDefault() != null,"is_default",deliveryMethodEntity.getIsDefault());
            wrapper.eq(deliveryMethodEntity.getCompanyId() != null,"company_id",deliveryMethodEntity.getCompanyId());
			wrapper.eq(deliveryMethodEntity.getStatus() != null,"status",deliveryMethodEntity.getStatus());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<DeliveryMethodDto> pageDeliveryMethod(Page<DeliveryMethodDto> page, DeliveryMethodDto deliveryMethodDto) {
        QueryWrapper<DeliveryMethodEntity> wrapper = getWrapper(deliveryMethodDto);
        Page<DeliveryMethodEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<DeliveryMethodEntity> deliveryMethodPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<DeliveryMethodDto> pageDto = new Page(deliveryMethodPage.getCurrent(), deliveryMethodPage.getSize(),deliveryMethodPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(deliveryMethodPage.getRecords(),DeliveryMethodDto.class));
        return pageDto;
    }

    @Override
    public IPage<DeliveryMethodDto> pageDeliveryMethodExtends(Page<DeliveryMethodDto> page, DeliveryMethodDto deliveryMethodDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        DeliveryMethodEntity deliveryMethodEntity = ConvertUtil.transformObj(deliveryMethodDto,DeliveryMethodEntity.class);
        List<DeliveryMethodEntity> deliveryMethodList=baseMapper.extendsList(pageMap,deliveryMethodEntity);
        Long total = baseMapper.extendsCount(deliveryMethodEntity);
        Page<DeliveryMethodDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(deliveryMethodList,DeliveryMethodDto.class));
        return pageDto;
    }
    @Override
    public DeliveryMethodDto getByIdExtends(Long id){
        DeliveryMethodEntity  deliveryMethodEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(deliveryMethodEntity,DeliveryMethodDto.class);
    }

    @Override
    public List<DeliveryMethodDto> listDeliveryMethod(DeliveryMethodDto deliveryMethodDto){
        QueryWrapper<DeliveryMethodEntity> wrapper = getWrapper(deliveryMethodDto);
        List<DeliveryMethodEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,DeliveryMethodDto.class);
    }
}

